package com.ruoyi.project.module.purchases.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.constant.ErpEnums;
import com.ruoyi.common.utils.UserUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.module.products.domain.Products;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.purchases.domain.Purchases;
import com.ruoyi.project.module.purchases.service.IPurchasesService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 国内采购 信息操作处理
 *
 * @author lcx
 * @date 2018-09-05
 */
@Controller
@RequestMapping("/module/purchases")
public class PurchasesController extends BaseController {
    private String prefix = "module/purchases";
    private String prefix3 = "module/courierSelect";


    @Autowired
    private IPurchasesService purchasesService;
    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("module:purchases:view")
    @GetMapping()
    public String purchases() {
        return prefix + "/purchases";
    }

    @RequiresPermissions("module:purchases:view_overseas")
    @GetMapping("/overseas")
    public String purchases_overseas(ModelMap mmap) {
        mmap.put("depList", deptService.selectDeptAll());
        return prefix + "/purchases_overseas";
    }

    @RequiresPermissions("module:purchases:view_received")
    @GetMapping("/received")
    public String purchases_received() {
        return prefix + "/purchases_received";
    }


    /**
     * @Author lcx
     * @Date 2018/9/23 12:37
     * 跳转到物流查询
     */
    @RequiresPermissions("module:courierSelect:view")
    @GetMapping("/courierSelect")
    public String courierSelect() {
        return prefix3 + "/courierSelect";
    }

    /**
     * 查询国内采购列表
     */
    @RequiresPermissions("module:purchases:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Purchases purchases) {
        purchases.setCompanyId(UserUtils.getDepId());
        //如果是广州
        if (UserUtils.GZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10302");
        }
        //深圳仓
        if (UserUtils.SZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10301");
        }
        startPageChange();
        List<Purchases> list = purchasesService.selectPurchasesList(purchases, ErpEnums.Delivered_state.WFH.getValue());
        /*查询合计*/
        List<Purchases> sumCountList = purchasesService.selectSumCountPurchasesList(purchases,ErpEnums.Delivered_state.WFH.getValue());

        return getDataTableAsSum(list,sumCountList);
    }
    /**
     * 查询发往海外仓
     */
    @RequiresPermissions("module:purchases:list_overseas")
    @PostMapping("/list_overseas")
    @ResponseBody
    public TableDataInfo list_overseas(Purchases purchases,String selCompanyId) {
        purchases.setCompanyId(UserUtils.getDepId());

        //如果是广州
        if (UserUtils.GZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10302");
        }
        if (UserUtils.SZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10301");
        }
        //如果特殊处理的公司有筛选权限
        if (!(selCompanyId == null || "100".equals(selCompanyId))) {
            purchases.setCompanyId(selCompanyId);
        }
        startPageChange();
        List<Purchases> list = purchasesService.selectPurchasesList(purchases, ErpEnums.Delivered_state.YFH.getValue());

        List<Purchases> sumCountList = purchasesService.selectSumCountPurchasesList(purchases,ErpEnums.Delivered_state.YFH.getValue());

        return getDataTableAsSum(list, sumCountList);
    }

    /**
     * @Author lcx
     * @Date 2018/9/30 15:44
     * 物流查询
     */
    @RequiresPermissions("module:courierSelect:list")
    @PostMapping("/list_courierSelect")
    @ResponseBody
    public TableDataInfo list_courierSelect(Purchases purchases) {
        purchases.setCompanyId(UserUtils.getDepId());
        //如果是广州
        if (UserUtils.GZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10302");
        }
        if (UserUtils.SZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10301");
        }
        startPageChange();
        List<Purchases> list = purchasesService.selectPurchasesList(purchases, ErpEnums.Delivered_state.YFH.getValue());

        List<Purchases> sumCountList = purchasesService.selectSumCountPurchasesList(purchases,ErpEnums.Delivered_state.YFH.getValue());

        return getDataTableAsSum(list, sumCountList);
    }
    /**
     * 查询海外仓签收
     */
    @RequiresPermissions("module:purchases:list_received")
    @PostMapping("/list_received")
    @ResponseBody
    public TableDataInfo list_received(Purchases purchases) {
        purchases.setCompanyId(UserUtils.getDepId());
        startPageChange();
        List<Purchases> list = purchasesService.selectPurchasesList(purchases, ErpEnums.Delivered_state.YQS.getValue());

        //List<Purchases> sumCountList = purchasesService.selectSumCountPurchasesList(purchases,ErpEnums.Delivered_state.YQS.getValue());

        return getDataTable(list);
    }

    /**
     * 根据运单号查询商品
     * 点击海外仓签收所用
     */
    @PostMapping("/list_edit_received")
    @ResponseBody
    public TableDataInfo list_edit_received(Purchases purchases) {
        purchases.setCompanyId(UserUtils.getDepId());
        //如果是广州
        if (UserUtils.GZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10302");
        }
        if (UserUtils.SZC_ID.equals(purchases.getCompanyId())){
            purchases.setCompanyId("");
            purchases.setChinaStorageID("10301");
        }
        List<Purchases> list = purchasesService.selectPurchasesListAsEditReceived(purchases);
        return getDataTable(list);
    }

    /**
     * 新增国内采购
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存国内采购
     */
    @RequiresPermissions("module:purchases:add")
    @Log(title = "国内采购", action = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Purchases purchases,
                              @RequestParam(value = "productIdList[]") String[] productIdList,
                              @RequestParam(value = "purchaseQuantityList[]") String[] purchaseQuantityList) {
        User user = ShiroUtils.getUser();
        purchases.setCreateId(String.valueOf(user.getUserId()));
        purchases.setCompanyId(String.valueOf(user.getDeptId()));
        purchases.setProductIdList_Tem(productIdList);
        purchases.setPurchaseQuantityList_Tem(purchaseQuantityList);
        return toAjax(purchasesService.insertPurchases(purchases));
    }

    /**
     * 修改国内采购
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Purchases purchases = purchasesService.selectPurchasesById(id);
        mmap.put("purchases", purchases);
        return prefix + "/edit";
    }

    /**
     * 修改发往海外仓库
     */
    @GetMapping("/edit_overseas/{id}")
    public String edit_overseas(@PathVariable("id") Integer id, ModelMap mmap) {
        Purchases purchases = purchasesService.selectPurchasesById(id);
        mmap.put("purchases", purchases);
        return prefix + "/edit_overseas";
    }

    /**
     * 修改发往海外仓库批量
     */
    @RequiresPermissions("module:purchases:edit")
    @GetMapping("/edit_overseas_batch/{ids}")
    public String edit_overseas_batch(@PathVariable("ids") String ids, ModelMap mmap) {

        mmap.put("ids", ids);

        return prefix + "/edit_overseas_batch";
    }
    /**
     * 修改海外仓签收
     */
    @RequiresPermissions("module:purchases:edit_received")
    @GetMapping("/edit_received/{overseasExpressNo}/{overseasExpressCompanyID}")
    public String edit_received(@PathVariable("overseasExpressNo") String overseasExpressNo,
                                @PathVariable("overseasExpressCompanyID") String overseasExpressCompanyID, ModelMap mmap) {

        mmap.put("overseasExpressNo", overseasExpressNo);
        mmap.put("overseasExpressCompanyID", overseasExpressCompanyID);

        return prefix + "/edit_received";
    }

    /**
     * 修改保存国内采购
     */
    @RequiresPermissions("module:purchases:edit")
    @Log(title = "国内采购", action = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Purchases purchases) {
        User user = ShiroUtils.getUser();
        purchases.setUpdateId(String.valueOf(user.getUserId()));
        return toAjax(purchasesService.updatePurchases(purchases));
    }

    /**
     * 修改保存发往海外仓
     */
    @RequiresPermissions("module:purchases:edit_overseas")
    @Log(title = "发往海外仓", action = BusinessType.UPDATE)
    @PostMapping("/edit_overseas")
    @ResponseBody
    public AjaxResult editSave_overseas(Purchases purchases) {
        //获取发货状态 为空代表 是国内采购的操作 1代表是 发往海外仓  2 代表是 海外签收
        String chinaReceived_state = purchases.getChinaReceived();

        if ("1".equals(chinaReceived_state)) {
            User user = ShiroUtils.getUser();
            //发货人 id
            purchases.setChinaReceiveId(String.valueOf(user.getUserId()));
        }
        return toAjax(purchasesService.updatePurchasesOverseas(purchases));
    }

    /**
     * 修改保存发往海外仓批量
     */
    @RequiresPermissions("module:purchases:edit_overseas")
    @Log(title = "发往海外仓批量", action = BusinessType.UPDATE)
    @PostMapping("/edit_overseas_batch")
    @ResponseBody
    public AjaxResult editSave_batch(Purchases purchases, String ids) {
        //获取发货状态 为空代表 是国内采购的操作 1代表是 发往海外仓  2 代表是 海外签收
        String chinaReceived_state = purchases.getChinaReceived();

        String id[] = ids.split(",");

        if ("1".equals(chinaReceived_state)) {
            User user = ShiroUtils.getUser();
            //发货人 id
            purchases.setChinaReceiveId(String.valueOf(user.getUserId()));
        }
        return toAjax(purchasesService.updatePurchases(id,purchases));
    }
    /**
     * 修改保存发往海外仓签收
     */
    @RequiresPermissions("module:purchases:edit_received")
    @Log(title = "海外仓签收", action = BusinessType.UPDATE)
    @PostMapping("/edit_received")
    @ResponseBody
    public AjaxResult editSave_received(
                                        @RequestParam(value = "ids[]")String[] ids,
                                        @RequestParam(value = "overseasReceiveQuantity[]")String[] overseasReceiveQuantity) {
        Purchases purchases = new Purchases();
        purchases.setChinaReceived(String.valueOf(ErpEnums.Delivered_state.YQS.getValue()));

        return toAjax(purchasesService.updatePurchasesAsReceived(purchases,ids,overseasReceiveQuantity));
    }


    /**
     * 删除国内采购
     */
    @RequiresPermissions("module:purchases:remove")
    @Log(title = "国内采购", action = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(purchasesService.deletePurchasesByIds(ids));
    }

    /**
     * 单个打印以及批量打印方法
     */
    @GetMapping("/print_label/{ids}/{createDates}")
    public String print_label(@PathVariable("ids") String ids,
                              @PathVariable("createDates") String createDates,
                              ModelMap mmap) {

        List <Purchases> purchasesList = purchasesService.selectProductAsPrintLabel(ids);
        mmap.put("purchasesList", purchasesList);

        return prefix + "/print_label";
    }

}
